package com.changhai.diabetes.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.changhai.diabetes.model.LabLipids;
import com.changhai.diabetes.model.LabPhysical;
import com.changhai.diabetes.model.LabSugar;
import com.changhai.diabetes.model.LabUrine;
import com.changhai.diabetes.service.JianyanService;
import com.changhai.diabetes.util.Constants;

/**
 * 检验随访
 * 
 * @author Andy
 * 
 */
public class JianyanServiceImpl extends CommonServiceImpl implements JianyanService {
	public LabPhysical searchPhyByPatientId(Long patientid) {
		Object o = this.getObject(Constants.DS_CHANGHAI, "labPhysical.getByPatientId", patientid);
		if (o != null && o instanceof LabPhysical) {
			return (LabPhysical) o;
		} else {
			LabPhysical p = new LabPhysical();
			p.setPatientid(patientid);
			return p;
		}
	}

	public LabSugar searchSugarByPatientId(Long patientid) {
		List<LabSugar> list = new ArrayList<LabSugar>();
		list = this.getList(Constants.DS_CHANGHAI, "labSugar.getByPatientId", patientid);
		if (list != null && list.size() > 0) {
			return (LabSugar) list.get(0);
		} else {
			LabSugar o = new LabSugar();
			o.setPatientid(patientid);
			o.setTestmethod(1);
			o.setTesttime(1);
			return o;
		}
	}

	public LabLipids searchLipidsByPatientId(Long patientid) {
		Object o = this.getObject(Constants.DS_CHANGHAI, "labLipids.getByPatientId", patientid);
		if (o != null && o instanceof LabLipids) {
			return (LabLipids) o;
		} else {
			LabLipids b = new LabLipids();
			b.setPatientid(patientid);
			return b;
		}
	}

	public LabUrine searchUrineByPatientId(Long patientid) {
		Object o = this.getObject(Constants.DS_CHANGHAI, "labUrine.getByPatientId", patientid);
		if (o != null && o instanceof LabUrine) {
			return (LabUrine) o;
		} else {
			LabUrine b = new LabUrine();
			b.setPatientid(patientid);
			return b;
		}
	}

	public void saveEditLabPhy(LabPhysical labphy) {
		if (labphy != null) {
			if (labphy.getPhysicalid() != null) {
				this.updateObject(Constants.DS_CHANGHAI, "labPhysical.updateByPrimaryKey", labphy);
			} else {
				Long phyid = this.saveNewLabPhy(labphy);
				if (phyid != null && phyid > 0) {
					labphy.setPhysicalid(phyid);
					this.updateObject(Constants.DS_CHANGHAI, "labPhysical.updateByPrimaryKey", labphy);
				}
			}
		}
	}

	private Boolean searchIfExits(LabSugar labsugar) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("patientid", labsugar.getPatientid());
		map.put("testtime", labsugar.getTesttime());
		map.put("testmethod", labsugar.getTestmethod());
		Object o = this.getObject(Constants.DS_CHANGHAI, "labSugar.getByTimeAndType", map);
		if (o != null && o instanceof LabSugar) {
			return true;
		} else {
			return false;
		}
	}

	public void saveEditLabSugar(LabSugar labsugar) {
		if (labsugar != null) {
			if (labsugar.getSugarid() != null) {
				if (searchIfExits(labsugar)) {
					this.updateObject(Constants.DS_CHANGHAI, "labSugar.updateByPrimaryKey", labsugar);
				} else {
					labsugar.setSugarid(null);
					Long id = this.saveNewLabSugar(labsugar);
					if (id != null && id > 0) {
						labsugar.setSugarid(id);
					}
				}
			} else {
				Long sugarid = this.saveNewLabSugar(labsugar);
				if (sugarid != null && sugarid > 0) {
					labsugar.setSugarid(sugarid);
					this.updateObject(Constants.DS_CHANGHAI, "labSugar.updateByPrimaryKey", labsugar);
				}
			}
		}
	}

	public void saveEditLabLipids(LabLipids lablipids) {
		if (lablipids != null) {
			if (lablipids.getLipidsid() != null) {
				this.updateObject(Constants.DS_CHANGHAI, "labLipids.updateByPrimaryKey", lablipids);
			} else {
				Long lipidsid = this.saveNewLabLipids(lablipids);
				if (lipidsid != null && lipidsid > 0) {
					lablipids.setLipidsid(lipidsid);
					this.updateObject(Constants.DS_CHANGHAI, "labLipids.updateByPrimaryKey", lablipids);
				}
			}
		}
	}

	public void saveEditLabUrine(LabUrine labUrine) {
		if (labUrine != null) {
			if (labUrine.getUrineid() != null) {
				this.updateObject(Constants.DS_CHANGHAI, "labUrine.updateByPrimaryKey", labUrine);
			} else {
				Long urineid = this.saveNewLabUrine(labUrine);
				if (urineid != null && urineid > 0) {
					labUrine.setUrineid(urineid);
					this.updateObject(Constants.DS_CHANGHAI, "labUrine.updateByPrimaryKey", labUrine);
				}
			}
		}
	}

	public Long saveNewLabPhy(LabPhysical labphy) {
		Integer id = 0;
		if (labphy != null && labphy.getPatientid() != null) {
			id = this.insertObjectID(Constants.DS_CHANGHAI, "labPhysical.insert", labphy);
		}
		return new Long(id);
	}

	public Long saveNewLabSugar(LabSugar labsugar) {
		Integer id = 0;
		if (labsugar != null && labsugar.getPatientid() != null) {
			id = this.insertObjectID(Constants.DS_CHANGHAI, "labSugar.insert", labsugar);
		}
		return new Long(id);
	}

	public Long saveNewLabLipids(LabLipids lablipids) {
		Integer id = 0;
		if (lablipids != null && lablipids.getPatientid() != null) {
			id = this.insertObjectID(Constants.DS_CHANGHAI, "labLipids.insert", lablipids);
		}
		return new Long(id);
	}

	public Long saveNewLabUrine(LabUrine laburine) {
		Integer id = 0;
		if (laburine != null && laburine.getPatientid() != null) {
			id = this.insertObjectID(Constants.DS_CHANGHAI, "labUrine.insert", laburine);
		}
		return new Long(id);
	}
}
